//
//  CommentView.m
//  仿微信朋友圈
//
//  Created by dllo on 16/9/3.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "CommentView.h"
#import "SDAutoLayout.h"
#import "CommentTableViewCell.h"
#import "CommentTableViewHeadView.h"
#import "UIView+Frame.h"
#import "SceneObj.h"
#import "SceneUserlist.h"
#import "SceneCommentlist.h"
#import "SceneStorylist.h"
#import "SCeneCommentcountlist.h"
#import "ScenePraiseinfolist.h"
#import "ScenePraisecontainerlist.h"
#import "MLLinkLabel.h"



@interface CommentView ()<MLLinkLabelDelegate>
@property (nonatomic, strong) UIImageView *likeImageView;
@property (nonatomic, strong) UIImageView *commentImageView;
@property (nonatomic, strong) UIView *grayView;
@property (nonatomic, strong) UIImageView *heartImageView;
@property (nonatomic, strong) UILabel *praiseLabel;
@property (nonatomic, strong) UIView *likeLineView;
@property (nonatomic, strong) NSMutableArray *labelsArr;

// 测试新的朋友圈
@property (nonatomic, strong) MLLinkLabel *likeLabel;
@property (nonatomic, strong) NSArray *likeItemsArray;
@property (nonatomic, strong) NSArray *commentItemsArray;
@property (nonatomic, strong) NSMutableArray *commentLabelsArray;
@end

@implementation CommentView{
    CGFloat tableViewHeight;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.labelsArr = [NSMutableArray array];
        [self createComment];
        
    }
    return self;
}



- (void)createComment {
    // 创建竖条View
    UIView *viewOfline = [UIView new];
    viewOfline.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    [self addSubview:viewOfline];
    
    self.likeImageView = [UIImageView new];
    self.commentImageView = [UIImageView new];
    self.numberLikeLabel = [UILabel new];
    self.numberCommentLabel = [UILabel new];
    self.grayView = [UIView new];
    self.grayView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1.000];
    [self addSubview:_likeImageView];
    [self addSubview:_commentImageView];
    [self addSubview:_numberLikeLabel];
    [self addSubview:_numberCommentLabel];
    [self addSubview:_grayView];
    self.numberCommentLabel.font = [UIFont systemFontOfSize:13];
    self.numberLikeLabel.font = [UIFont systemFontOfSize:13];
    self.numberLikeLabel.textColor = [UIColor colorWithWhite:0.702 alpha:1.000];
    self.numberCommentLabel.textColor = [UIColor colorWithWhite:0.702 alpha:1.000];
    self.likeImageView.image = [UIImage imageNamed:@"Indexnew_love"];
    self.commentImageView.image = [UIImage imageNamed:@"index_jilu_more"];
    
    self.heartImageView = [UIImageView new];
    self.likeUserNameLabel = [UILabel new];
    self.praiseLabel = [UILabel new];
    self.likeLineView = [UIView new];
    self.commentUserNameLabel = [UILabel new];
    self.commentLabel = [UILabel new];

    [_grayView addSubview:_heartImageView];
    [_grayView addSubview:_likeUserNameLabel];
    [_grayView addSubview:_praiseLabel];
    [_grayView addSubview:_likeLineView];
    [_grayView addSubview:_commentUserNameLabel];
    [_grayView addSubview:_commentLabel];
    
    self.numberCommentLabel.sd_layout
    .rightSpaceToView(self, 10)
    .topSpaceToView(self, 5)
    .heightIs(30)
    .widthIs(10);
    self.numberCommentLabel.text = @"0";
    self.numberLikeLabel.text = @"0";
    self.commentImageView.sd_layout
    .centerYEqualToView(self.numberCommentLabel)
    .rightSpaceToView(self.numberCommentLabel, 2)
    .widthIs(25)
    .heightIs(15);
    
    viewOfline.sd_layout
    .widthIs(1)
    .heightIs(15)
    .centerYEqualToView(self.commentImageView)
    .rightSpaceToView(self.commentImageView, 6);
    
    self.numberLikeLabel.sd_layout
    .heightIs(30)
    .widthIs(10)
    .rightSpaceToView(viewOfline, 6)
    .centerYEqualToView(viewOfline);
    
    self.likeImageView.sd_layout
    .widthIs(30)
    .heightIs(25)
    .rightSpaceToView(self.numberLikeLabel, 2)
    .centerYEqualToView(self.numberLikeLabel).offset(3);
    
    // 创建评论和赞的灰色背景
    self.grayView = [UIView new];
    self.grayView.backgroundColor = [UIColor colorWithWhite:0.957 alpha:1.000];
    [self addSubview:_grayView];
    _grayView.sd_layout
    .leftSpaceToView(self, 5)
    .topSpaceToView(self.likeImageView, 5)
    .rightSpaceToView(self, 5)
    .heightIs(200);
    // 创建赞
//    self.heartImageView = [UIImageView new];
//    _heartImageView.image = [UIImage imageNamed:@"praise_icon"];
//    [_grayView addSubview:_heartImageView];
//    _heartImageView.sd_layout
//    .leftSpaceToView(_grayView, 10)
//    .topSpaceToView(_grayView, 10)
//    .heightIs(20)
//    .widthIs(20);
    // 创建点赞的用户名label
    _likeLabel = [MLLinkLabel new];
    _likeLabel.font = [UIFont systemFontOfSize:14];
    UIColor *heighlightColor = [UIColor colorWithRed:92/255.0 green:140/255.0 blue:193/255.0 alpha:1.0];
    _likeLabel.linkTextAttributes = @{NSForegroundColorAttributeName : heighlightColor};
    _likeLabel.isAttributedContent = YES;
    [self.grayView addSubview:_likeLabel];

    
    // 创建横杠
    self.likeLineView = [UIView new];
    [self.grayView addSubview:_likeLineView];
//    _likeLineView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
//    _likeLineView.sd_layout
//    .leftSpaceToView(self.grayView, 6)
//    .rightSpaceToView(self.grayView, 6)
//    .topSpaceToView(self.likeLabel, 10)
//    .heightIs(1);
    
    // 创建评论的内容和用户名的拼接的label
   
}

#pragma mark - 新的拼接
- (void)setCommentItemsArray:(NSArray *)commentItemsArray
{
    _commentItemsArray = commentItemsArray;
    
    long originalLabelsCount = self.commentLabelsArray.count;
    long needsToAddCount = commentItemsArray.count > originalLabelsCount ? (commentItemsArray.count - originalLabelsCount) : 0;
    for (int i = 0; i < needsToAddCount; i++) {
        MLLinkLabel *label = [MLLinkLabel new];
        UIColor *highLightColor = [UIColor colorWithRed:92/255.0 green:140/255.0 blue:193/255.0 alpha:1.0];;
        label.linkTextAttributes = @{NSForegroundColorAttributeName : highLightColor};
        label.font = [UIFont systemFontOfSize:14];
        label.delegate = self;
        [self addSubview:label];
        [self.commentLabelsArray addObject:label];
    }
    
    for (int i = 0; i < commentItemsArray.count; i++) {
       SceneCommentlist *model = commentItemsArray[i];
        MLLinkLabel *label = self.commentLabelsArray[i];
        if (!model.attributedContent) {
            model.attributedContent = [self generateAttributedStringWithCommentItemModel:model];
        }
        label.attributedText = model.attributedContent;
    }
}

- (void)setLikeItemsArray:(NSArray *)likeItemsArray
{
    _likeItemsArray = likeItemsArray;
    
    NSTextAttachment *attach = [NSTextAttachment new];
    attach.image = [UIImage imageNamed:@"index_jilu_love_s"];
    attach.bounds = CGRectMake(0, -3, 16, 16);
    NSAttributedString *likeIcon = [NSAttributedString attributedStringWithAttachment:attach];
    
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:likeIcon];
    
    for (int i = 0; i < likeItemsArray.count; i++) {
        ScenePraiseinfolist *model = likeItemsArray[i];
        if (i > 0) {
            [attributedText appendAttributedString:[[NSAttributedString alloc] initWithString:@"，"]];
        }
        if (!model.attributedContent) {
            model.attributedContent = [self generateAttributedStringWithLikeItemModel:model];
        }
        [attributedText appendAttributedString:model.attributedContent];
    }
    
    _likeLabel.attributedText = [attributedText copy];
}

- (NSMutableArray *)commentLabelsArray
{
    if (!_commentLabelsArray) {
        _commentLabelsArray = [NSMutableArray new];
    }
    return _commentLabelsArray;
}

- (void)setupWithLikeItemsArray:(NSArray *)likeItemsArray commentItemsArray:(NSArray *)commentItemsArray
{
    self.likeItemsArray = likeItemsArray;
    self.commentItemsArray = commentItemsArray;
    
    if (self.commentLabelsArray.count) {
        [self.commentLabelsArray enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop) {
            [label sd_clearAutoLayoutSettings];
            label.hidden = YES; //重用时先隐藏所以评论label，然后根据评论个数显示label
        }];
    }
    
    if (!commentItemsArray.count && !likeItemsArray.count) {
        self.fixedWidth = @(0); // 如果没有评论或者点赞，设置commentview的固定宽度为0（设置了fixedWith的控件将不再在自动布局过程中调整宽度）
        self.fixedHeight = @(0); // 如果没有评论或者点赞，设置commentview的固定高度为0（设置了fixedHeight的控件将不再在自动布局过程中调整高度）
        return;
    } else {
        self.fixedHeight = nil; // 取消固定宽度约束
        self.fixedWidth = nil; // 取消固定高度约束
    }
    
    CGFloat margin = 5;
    
    UIView *lastTopView = nil;
    
    if (likeItemsArray.count) {
        _likeLabel.sd_resetLayout
        .leftSpaceToView(self, margin)
        .rightSpaceToView(self, margin)
        .topSpaceToView(lastTopView, 10)
        .autoHeightRatio(0);
        
        lastTopView = _likeLabel;
    } else {
        _likeLabel.attributedText = nil;
        _likeLabel.sd_resetLayout
        .heightIs(0);
    }
    
    
    if (self.commentItemsArray.count && self.likeItemsArray.count) {
        _likeLineView.sd_resetLayout
        .leftSpaceToView(self.grayView, 0)
        .rightSpaceToView(self.grayView, 0)
        .heightIs(1)
        .topSpaceToView(lastTopView, 3);
        
        lastTopView = _likeLineView;
    } else {
        _likeLineView.sd_resetLayout.heightIs(0);
    }
    
    for (int i = 0; i < self.commentItemsArray.count; i++) {
        UILabel *label = (UILabel *)self.commentLabelsArray[i];
        label.hidden = NO;
        CGFloat topMargin = (i == 0 && likeItemsArray.count == 0) ? 10 : 5;
        label.sd_layout
        .leftSpaceToView(self, 8)
        .rightSpaceToView(self, 5)
        .topSpaceToView(lastTopView, topMargin)
        .autoHeightRatio(0);
        
        label.isAttributedContent = YES;
        lastTopView = label;
    }
    
    [self.grayView setupAutoHeightWithBottomView:lastTopView bottomMargin:6];
    [self setupAutoHeightWithBottomView:self.grayView bottomMargin:10];
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
}

#pragma mark - private actions

- (NSMutableAttributedString *)generateAttributedStringWithCommentItemModel:(SceneCommentlist *)model
{
    NSInteger userId = model.userId;
    NSString *text = @"";
    NSMutableAttributedString *attString = nil;
    for (SceneUserlist *usetList in _userList) {
        if (userId == usetList.userId) {
             text = usetList.userName;
            text = [text stringByAppendingFormat:@": %@", model.context];
            
            NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:text];
            [attString setAttributes:@{NSLinkAttributeName : [NSString stringWithFormat:@"%ld", usetList.userId]} range:[text rangeOfString:usetList.userName]];
           
        }
    }
   
    
       return attString;
}

- (NSMutableAttributedString *)generateAttributedStringWithLikeItemModel:(ScenePraiseinfolist *)model
{
    NSString *text = model.userName;
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:text];
    UIColor *highLightColor = [UIColor blueColor];
    [attString setAttributes:@{NSForegroundColorAttributeName : highLightColor, NSLinkAttributeName : [NSString stringWithFormat:@"%ld", model.userId]} range:[text rangeOfString:model.userName]];
    
    return attString;
}


#pragma mark - 
- (void)didClickLink:(MLLink *)link linkText:(NSString *)linkText linkLabel:(MLLinkLabel *)linkLabel
{
    NSLog(@"点了");
}
















#pragma mark - 我写的set方法, 有问题
//- (void)setPraiseArr:(NSArray *)praiseArr {
//   
//    _praiseArr = praiseArr;
//    self.likeUserNameLabel = [UILabel new];
//    self.likeUserNameLabel.font = [UIFont systemFontOfSize:13];
//    
//     NSString *praiseText = @"";
//    
//    ScenePraiseinfolist *lastPraiseList = praiseArr.lastObject;
//    for (int i = 0 ; i < praiseArr.count - 1; i++) {
//        ScenePraiseinfolist *praiseList = praiseArr[i];
//        ScenePraiseinfolist *praiseList2 = praiseArr[i + 1];
//        NSString *currentStr = praiseList.userName;
//        NSString *nextStr = praiseList2.userName;
//        praiseText = [currentStr stringByAppendingFormat:@",%@", nextStr];
//        
//    }
//    if (praiseArr.count == 0) {
//        
//    }else if (praiseArr.count == 1) {
//        _likeUserNameLabel.text = [NSString stringWithFormat:@"%@  赞", lastPraiseList.userName];
//    } else {
//    _likeUserNameLabel.text = [NSString stringWithFormat:@"%@  赞", praiseText];
//    }
//    [self.grayView addSubview:_likeUserNameLabel];
//    _likeUserNameLabel.sd_layout
//    .leftSpaceToView(self.heartImageView, 0)
//    .rightSpaceToView(self.grayView, 10)
//    .topSpaceToView(self.grayView, 17)
//    .autoHeightRatio(0);
//    
//}
//- (void)setCommentArr:(NSArray *)commentArr {
//    _commentArr = commentArr;
//    if (commentArr.count == 0) {
//        [self.grayView setupAutoHeightWithBottomView:self.likeLineView bottomMargin:10];
//    } else {
//        for (int i = 0; i < commentArr.count; i++) {
//        UILabel *label = [UILabel new];
//        label.text = commentArr[i];
//        [self.grayView addSubview:label];
//        [self.labelsArr addObject:label];
//        }
//        UIView *lastTopView = self.likeLineView;
//        for (int i = 0; i < self.labelsArr.count; i++) {
//            UILabel *label = (UILabel *)self.labelsArr[i];
//            label.hidden = NO;
//            CGFloat topMargin = (i == 0) ? 10 : 5;
//            label.sd_layout
//            .leftSpaceToView(self, 8)
//            .rightSpaceToView(self, 5)
//            .topSpaceToView(lastTopView, topMargin)
//            .autoHeightRatio(0);
//            label.isAttributedContent = YES;
//            lastTopView = label;
//        }
//    [self.grayView setupAutoHeightWithBottomView:lastTopView bottomMargin:6];
////    for (int i = 0; i < commentArr.count; i++) {
////        self.commentLabel = [UILabel new];
////        [self.grayView addSubview:_commentLabel];
////        _commentLabel.font = [UIFont systemFontOfSize:13];
////        _commentLabel.text = @"用户名:拼接评论的内容, 且用户名颜色为蓝色";
////        _commentLabel.tag = i;
////        NSMutableArray *labelArr = [NSMutableArray array];
////               UILabel *lastLabel = [self.grayView viewWithTag:commentArr.count - 1];
////        [self.grayView setupAutoHeightWithBottomViewsArray:labelArr bottomMargin:10];
//        [self setupAutoHeightWithBottomView:self.grayView bottomMargin:10];
//         self.refreshControl = [[UIRefreshControl alloc] init];
//        [self.refreshControl addTarget:self action:@selector(refreshAction:) forControlEvents:UIControlEventValueChanged];
//
//    }
//   
//}

@end
